/*
 * find_latest_segment_file - seek for the given segment file 
 *                            that is last modified in the given position,
 *                            according to the last modified time
 * @param  uri       the place where is the filesystem palced
 * @param  fs_name   the directory named fs_name
 * @return Return the inode of the last modified file
 */
	 
int
find_latest_segment_file(char *uri, char *fs_name)
{
     DIR *dir, *dir1;
     struct dirent *ptr = NULL;
     struct dirent *ptr1 = NULL;
     time_t mtime_new = 0;                           //the newest mtime
     struct stat buf;
     int inode;                          //the inode of the segment file of the newest mtime file
	 //struct inode
		
	 dir = opendir(uri);
     if (dir == NULL)
	 {
	      perror("opendir error!");
          return EDIR;
	 }
	 
	 while( (ptr = readdir(dir)) != NULL)
	 {
	     if( strcmp(ptr->d_name, fs_name) == 0)
         {
	           dir1 = opendir(fs_name);
	           if (dir1 == NULL)
               {
	               perror("opendir error!");
	               return EDIR;
	           }
	           while( (ptr1 = readdir(dir1)) != NULL)
	           {
	              if( stat(ptr1, &buf) == -1)
	              {
	                    perror("stat error!");
	                    exit(1);
	              }
    	          if(mtime_new < buf.st_mtime)
                  {
	                  mtime_new = buf.st_mtime;
			          inode = buf.st_ino;
                  }
		       }
   	     }
	}
 
   if(ptr1 == NULL)
   {
	   printf("the directory is not exist!\n");
	   return EDIR;
   }
				 
   return inode;
}

